import javax.swing.border.TitledBorder;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static javax.swing.text.StyleConstants.getComponent;

// 路口面板
// 路口面板
// 路口面板
class IntersectionPanel extends JPanel {
    private static final Font INFO_FONT = new Font("宋体", Font.BOLD, 14);
    private static final Font TIMER_FONT = new Font("Arial", Font.BOLD, 18);
    private final EdgeDevice device;
    private Image bufferImage;
    private Graphics bufferGraphics;
    private static int [] vehicleCount={0,0,0,0};
    private int vehicleCount_index;
    public IntersectionPanel(EdgeDevice device,int vehicleCount_index) {
        this.vehicleCount_index=vehicleCount_index;
        this.device = device;
        setPreferredSize(new Dimension(535, 500));
        setBackground(new Color(240, 240, 240));

        String title = device.getDeviceId() + " - " + device.getLocation();
        if (device.isSchoolZone()) {
            title += " (学校区域)";
            setBorder(BorderFactory.createTitledBorder(
                    BorderFactory.createLineBorder(Color.RED, 2),
                    title,
                    TitledBorder.LEFT,
                    TitledBorder.TOP,
                    INFO_FONT,
                    Color.RED
            ));
        } else {
            title += " (市中心)";
            setBorder(BorderFactory.createTitledBorder(title));
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (bufferImage == null) {
            bufferImage = createImage(getWidth(), getHeight());
            bufferGraphics = bufferImage.getGraphics();
        }
        bufferGraphics.setColor(getBackground());
        bufferGraphics.fillRect(0, 0, getWidth(), getHeight());

        drawRoads(bufferGraphics);
        drawTrafficLights(bufferGraphics);
//        device.draw(bufferGraphics, 160, 220);
        device.draw(bufferGraphics, 200, 250);
        drawStatusInfo(bufferGraphics);
        drawVehicleCount(bufferGraphics); // 新增：绘制车辆数量统计信息

        if (device.isSchoolZone()) {
            bufferGraphics.setColor(new Color(255, 50, 50, 50));
            bufferGraphics.fillRect(0, 0, getWidth(), getHeight());
        }

        g.drawImage(bufferImage, 0, 0, this);
    }

    private void drawRoads(Graphics g) {
        g.setColor(new Color(80, 80, 80));
        g.fillRect(150, 0, 100, getHeight());
        g.fillRect(0, 200, getWidth(), 100);

        g.setColor(Color.YELLOW);
        g.drawLine(200, 0, 200, 200);
        g.drawLine(200, 300, 200, getHeight());
        g.drawLine(0, 250, 150, 250);
        g.drawLine(250, 250, getWidth(), 250);
    }

    private void drawTrafficLights(Graphics g) {
        EdgeDevice.LightState[] states = device.getLightStates();
        int[] timers = device.getLightTimers();

        drawLight(g, 105, 70, states[0], timers[0], "南向");
        drawLight(g, 105, 310, states[1], timers[1], "东向");
        drawLight(g, 255, 310, states[0], timers[0], "北向");
        drawLight(g, 255, 70, states[1], timers[1], "西向");
    }

    private void drawLight(Graphics g, int x, int y, EdgeDevice.LightState state, int timer, String label) {
        g.setColor(Color.DARK_GRAY);
        g.fillRect(x + 15, y + 100, 10, 25);

        g.setColor(Color.BLACK);
        g.fillRect(x, y, 40, 100);

        g.setColor(state == EdgeDevice.LightState.RED ? Color.RED : new Color(50, 0, 0));
        g.fillOval(x + 5, y + 10, 30, 30);

        g.setColor(state == EdgeDevice.LightState.YELLOW ? Color.YELLOW : new Color(50, 50, 0));
        g.fillOval(x + 5, y + 40, 30, 30);

        g.setColor(state == EdgeDevice.LightState.GREEN ? Color.GREEN : new Color(0, 50, 0));
        g.fillOval(x + 5, y + 70, 30, 30);

        g.setColor(Color.WHITE);
        g.setFont(TIMER_FONT);
        g.drawString(String.valueOf(timer), x + 11, y + 62);

        g.setColor(Color.BLACK);
        g.setFont(INFO_FONT);
        g.drawString(label, x + 8, y + 130);
    }

    private void drawStatusInfo(Graphics g) {
        EdgeDevice.LightState[] states = device.getLightStates();
        int[] timers = device.getLightTimers();

        g.setFont(INFO_FONT);
        g.setColor(Color.BLACK);
        g.drawString("控制策略: " + (device.isUsingFixedTiming() ? "固定" : "云端优化"), 300, 30);
        g.drawString("南向: " + states[0] + " (" + timers[0] + "秒)", 300, 50);
        g.drawString("东向: " + states[1] + " (" + timers[1] + "秒)", 300, 70);
        g.drawString("北向: " + states[0] + " (" + timers[0] + "秒)", 300, 90);
        g.drawString("西向: " + states[1] + " (" + timers[1] + "秒)", 300, 110);
    }

    // 新增：绘制车辆数量统计信息
    private void drawVehicleCount(Graphics g) {
        synchronized (device.getVehicles()) {
            for (Vehicle vehicle : device.getVehicles()) {
                // Check if vehicle has passed through the intersection
                if (!vehicle.count_flag) {
                    if ((vehicle.getLaneId() == 1 && vehicle.getY() >= 200 && vehicle.getY() < 300) ||
                            (vehicle.getLaneId() == 2 && vehicle.getX() >= 150 && vehicle.getX() < 250) ||
                            (vehicle.getLaneId() == 3 && vehicle.getY() <= 300 && vehicle.getY() > 200) ||
                            (vehicle.getLaneId() == 4 && vehicle.getX() <= 250 && vehicle.getX() > 150)) {

                        vehicleCount[vehicleCount_index]++;
                        vehicle.count_flag = true;
                    }
                }
            }
        }
        g.setFont(INFO_FONT);
        g.setColor(Color.BLACK);
        g.drawString("车辆数量: " + vehicleCount[vehicleCount_index], 300, 130);
    }
}